/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.JournalDao;
import com.qdu.entity.Journal;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Journal表数据库操作的实现代码
 * @author Lenovo
 */
public class JournalDaoImpl extends BaseDaoImpl1 implements JournalDao{

    @Override
    public List<Journal> getMyJournalOnIndex(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Journal> jonlist = new ArrayList<>();

        try {
            con =DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Jid,title,content,pubtime from Journal where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                jonlist.add(new Journal(rs.getString(1),rs.getString(2),rs.getString(3), rs.getTimestamp(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return jonlist;
    }
    
    
    @Override
    public List<Journal> getAllJournalOnIndex() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Journal> jonlist = new ArrayList<>();

        try {
            con =DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Jid,id,title,content,pubtime from Journal order by pubtime desc");
            rs = ps.executeQuery();
            while (rs.next()) {
                jonlist.add(new Journal(rs.getString(1), rs.getInt(2), rs.getString(3),rs.getString(4), rs.getTimestamp(5)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return jonlist;
    }
   
    @Override
    public List<Journal> getAllJournal() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Journal> jonlist = new ArrayList<>();

        try {
            con =DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Jid,id,title,pubtime from Journal");
            rs = ps.executeQuery();
            while (rs.next()) {
                jonlist.add(new Journal(rs.getString(1), rs.getInt(2), rs.getString(3), rs.getTimestamp(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return jonlist;
    }

    @Override
    public Journal getJournalById(String Jid) { 
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Journal jon = null;

        try {
            con =DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Journal where Jid=?");
            ps.setString(1, Jid);
            rs = ps.executeQuery();
            
           
            
            if (rs.next()) {
                jon=new Journal(rs.getString(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getTimestamp(5),rs.getInt(6));
            }
           
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return jon;
    }

    @Override
    public int addJournal(Journal jon) {
        System.out.println("Jid........."+jon.getJid());
        return executeUpdate("insert into Journal(Jid,id,title,content) values(?,?,?,?)"
                              ,jon.getJid()
                              ,jon.getId()
                              ,jon.getTitle()
                              ,jon.getContent()
        );
    }

    @Override
    public int editJournal(Journal jon) {
        return executeUpdate("update Journal set title=?,content=? where Jid=?"                       
                              ,jon.getTitle()
                              ,jon.getContent()
                              ,jon.getJid()
        );
    }

    @Override
    public int delJournalById(String Jid) {  
        return executeUpdate("delete Journal where Jid=?",Jid);   
    }

    @Override
    public List<Journal> getJournalByKeyword(String journalName) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Journal> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Jid,id,title,pubtime from Journal where title like ?");
            ps.setString(1, "%" + journalName + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Journal(rs.getString(1), rs.getInt(2), rs.getString(3), rs.getTimestamp(4))); 
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public List<Journal> getJournalById(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Journal> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Jid,id,title,pubtime from Journal where id like ?");
            ps.setString(1, "%" + id + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Journal(rs.getString(1), rs.getInt(2), rs.getString(3), rs.getTimestamp(4))); 
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
}
